package com.soso.confidant.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.qq.oauth.Token;
import com.qq.util.App;
import com.qq.util.ParseString;

public class LoginAction implements ServletRequestAware,ServletResponseAware{
	
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	public String execute() throws IOException {
		//PrintWriter out = response.getWriter();
		String appid = App.appid;
		String appkey = App.appkey;
		String oauth_callback = App.oauth_callback;
		String redirect_url = "http://openapi.qzone.qq.com/oauth/qzoneoauth_authorize?oauth_consumer_key="+appid;
		//request token
		Token token = new Token();
		String request_token = token.getRequestToken(appid, appkey);
		//out.println(request_token);
		HashMap<String, String> tokens = ParseString.ParseTokenString(request_token);
		String oauth_token = tokens.get("oauth_token");
		//out.println(oauth_token);
		String oauth_token_secret = tokens.get("oauth_token_secret");
		//out.println(oauth_token_secret);
		request.getSession().setAttribute("oauth_token_secret", oauth_token_secret);
		redirect_url += "&oauth_token="+oauth_token;
		redirect_url += "&oauth_callback="+oauth_callback;
		//out.println(redirect_url);
		response.sendRedirect(redirect_url);
		return null;
	}
	
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
}
